package com.ruoyi.recycleshop.job;

import com.alibaba.fastjson.JSON;
import com.ruoyi.recycle.domain.Coupon;
import com.ruoyi.recycle.service.CouponService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class CreateCouponJob implements Job {
    @Autowired
    private CouponService couponService;

    @Override
    @SneakyThrows
    public void execute(JobExecutionContext jobExecutionContext) {
        final JobDataMap mergedJobDataMap = jobExecutionContext.getJobDetail().getJobDataMap();
        final String createCoupon = mergedJobDataMap.getString("create_coupon");
        final boolean isExpire = mergedJobDataMap.getBoolean("isExpire");
        final Coupon coupon = JSON.parseObject(createCoupon, Coupon.class);
        log.info("优惠券有效期任务,优惠券ID:{},{}", coupon.getId(), isExpire ? "已失效" : "已生效");
        couponService.updateById(coupon.setStatus(isExpire ? "已失效" : "已生效"));

    }
}
